﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMSModel;

public partial class Topics_Statistics_Edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            string flag = Request["flag"];
            if(flag=="edit")
            {
                bd();
            }else if (flag == "delete")
            {
                string typeid =Request["typeid"];
                CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
                var c = dal.VoteType.First(x => x.TypeID == typeid);
                try
                {
                    dal.DeleteObject(c);
                    dal.SaveChanges();
                    ClientScript.RegisterStartupScript(GetType(), "delete", "alert('删除成功!'),document.location='Default.aspx'", true);
                }
                catch (Exception ex)
                {

                    LogYuMin.LogWriter.GetWriter(LogYuMin.LogFile.error).Write(GetType(), "DeleteNews", LogYuMin.LogStates.ERROR, ex);
                    ClientScript.RegisterStartupScript(GetType(), "delete", "alert('删除失败!')", true);
                }
            }
        }
    }

    void bd()
    {
        txtTypeId.Attributes.Add("readonly", "readonly");
        txtTypeId.BackColor = System.Drawing.Color.Gray;
        string typeid = Request["typeid"];
        CMSEntities cms = new CMSEntities();
        var c = cms.VoteType.First(x => x.TypeID == typeid);
        txtTypeId.Text = c.TypeID;
        txtTitle.Text = c.Title;
        txtQuestion.Text = c.Question;

        repeater1.DataSource = c.Vote;
        repeater1.DataBind();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string flag = Request["flag"];
        if (flag == "add")
        {

            VoteType v = new VoteType();
            v.Question = txtQuestion.Text;
            v.Title = txtTitle.Text;
            v.TypeID = txtTypeId.Text;
            CMSEntities cms = new CMSEntities();
            if (cms.VoteType.Any(x => x.TypeID == v.TypeID))
            {
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('已经存在这个类型编号')", true);
                return;
            }
            else
            {
                v.CTime = DateTime.Now;
                cms.VoteType.AddObject(v);
                cms.SaveChanges();
                Response.Redirect("Statistics.aspx");
            }
        }
        else if(flag=="edit")
        {
            CMSEntities cms = new CMSEntities();
            string typeid = txtTypeId.Text;
            var c = cms.VoteType.First(x => x.TypeID == typeid);
            c.Title = txtTitle.Text;
            c.CTime = DateTime.Now;
            c.Question = txtQuestion.Text;
            cms.SaveChanges();
        }
    }
}